package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayArea;
import org.itboys.core.service.mongo.web.SundayDistributionAreaService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/8/14
 * @Description：配送区域
 */
@Controller
@RequestMapping("/sunday/web/area")
public class SundayDistributionAreaController extends BaseController{

    @Autowired
    private SundayDistributionAreaService areaService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        return "/sunday/area/index";
    }
    @RequestMapping("/input" )
    public  String input(HttpServletRequest request
            ,@RequestParam("id") Long id
            ,Model model){
        model.addAttribute("area",areaService.findOne(id));
        return "/sunday/area/input";
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
        ,@RequestParam("id")Long id,@RequestParam("name")String name
        ,@RequestParam("storeId")Long storeId,@RequestParam("user")String user
        ,@RequestParam("mobile")String mobile){
        try {
            areaService.save(id,new SundayArea(name, storeId, user, mobile));
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/area/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request
            , HttpServletResponse response){
        try {
            PageResult<SundayArea> result = areaService.select(request);
            System.out.println(result.getData().toString());
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       @RequestParam(value="id",required=true) Long id){
        try {
            areaService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroeException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/area/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    /**
     * 区域选择器，目前业务逻辑只考虑单选
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/area/choose/choose_erro";
        }
        return "/sunday/area/choose/choose_single";
    }
}
